package com.google.code.sip.ucenter.mail;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 * MailService接口的实现.
 * 
 * @author Steven Liu
 */
public class DefaultMailService extends MailService {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private JavaMailSender mailSender;
	private static final int THREAD_POOL_SIZE = 2;

	private Executor executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	/**
	 * 发送SimpleMailMessage.
	 */
	@Override
	public void send(SimpleMailMessage msg) {
		try {
			mailSender.send(msg);
		} catch (MailException e) {
			logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 使用模版发送HTML格式的邮件.
	 * 
	 * @param msg
	 *            装有to,from,subject信息的SimpleMailMessage
	 * @param templateName
	 *            模版名,模版根路径已在配置文件定义于freemakarengine中
	 * @param model
	 *            渲染模版所需的数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void send(SimpleMailMessage msg, String templateName, Map model) {
		// 生成html邮件内容
		String content = generateEmailContent(templateName, model);
		MimeMessage mimeMsg = mailSender.createMimeMessage();
		;
		try {
			MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true,
					"utf-8");
			helper.setTo(msg.getTo());
			helper.setSubject(msg.getSubject());
			helper.setFrom(msg.getFrom());
			helper.setText(content, true);
			mailSender.send(mimeMsg);
		} catch (MessagingException ex) {
			logger.error(ex.getMessage(), ex);
		}
	}

	@SuppressWarnings("unchecked")
	public void send(SimpleMailMessage msg, String templateName, Map model,
			String[] receviers) {

		String content = generateEmailContent(templateName, model);
		for (String receiver : receviers) {
			SimpleMailMessage msg1 = new SimpleMailMessage();
			msg1.setFrom("webmaster@jobht.com");
			msg1.setTo(receiver);
			msg1.setSubject(msg.getSubject());
			msg1.setText(content);

			executor.execute(new MailTask(mailSender, msg1));
		}
	}

	/**
	 * 群发邮件任务类.
	 */
	private class MailTask implements Runnable {

		private JavaMailSender mailSender;
		private SimpleMailMessage msg;

		public MailTask(JavaMailSender mailSender, SimpleMailMessage msg) {
			this.mailSender = mailSender;
			this.msg = msg;
		}

		public void run() {
			try {
				mailSender.send(msg);
				logger.info("纯文本邮件已发送至" + msg.getTo());
			} catch (MailException e) {
				logger.error("发送邮件失败", e);
			}
		}
	}
}
